package com.htht.smartschool.util.message;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htht.smartschool.model.amq.Aps;
import com.htht.smartschool.model.amq.MessageData;
import com.htht.smartschool.model.amq.Payload;
import com.htht.smartschool.model.message.TMessage;
import com.htht.smartschool.model.push.MResult;
import com.htht.smartschool.model.user.TUser;
import com.htht.smartschool.service.teacher.TeacherService;
import com.htht.smartschool.service.user.UserService;
import com.htht.smartschool.util.http.HttpUtil;
import com.htht.smartschool.util.system.ConfigUtil;
import com.htht.smartschool.util.system.GsonUtils;

public class MessageUtil{
	private MessageUtil(){}
	static public Map<String,Integer> appids = new HashMap<String,Integer>();
	static public List<Integer> appidList;
	public static TMessage getMessageBody(TMessage me){
		UserService us = new UserService();
		me.setIdatetime(new Date().getTime()/1000);
		me.setContent(me.getContent());
		int a = Integer.parseInt(me.getType());
		List<Long> list = new ArrayList<Long>();
		Map<String,List<Long>> map = new HashMap<String, List<Long>>();
		String[] ids = new String[]{};
		switch (a) {
		case 1:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("teacherid"));
			appidList.add(appids.get("userid"));
			if(me.getReceivecode() != null && me.getReceivecode().length()>0){
				ids = me.getReceivecode().split(",");
			}
			if(ids.length>0){
				appidList = new ArrayList<Integer>();
				for (String id : ids) {
					appidList.add(Integer.parseInt(id));
				}
			}
			me.setName("新闻公告");
			me.setSendType(3);
			me.setFrom(0L);
			break;
		case 2:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("teacherid"));
			appidList.add(appids.get("userid"));
			if(me.getReceivecode() != null && me.getReceivecode().length()>0){
				ids = me.getReceivecode().split(",");
			}
			if(ids.length>0){
				appidList = new ArrayList<Integer>();
				for (String id : ids) {
					appidList.add(Integer.parseInt(id));
				}
			}
			me.setName("学校通知");
			me.setSendType(3);
			me.setFrom(0L);
			break;
		case 3:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("userid"));
			me.setName("到离校通知");
			me.setSendType(1);
			break;
		case 4:
			//需等待杨龙接口
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("userid"));
			me.setName("班内通知");
			me.setIdatetime(new Date().getTime()/1000);
//			me.setChannel(Integer.parseInt(me.getReceivecode()));
//			me.setSendType(2);
			//暂用对应发
			TeacherService ts = new TeacherService();
			List<TUser> listtu = ts.getUseridByClasscode(me.getReceivecode());
			for (TUser tu : listtu) {
				list.add(Long.valueOf(tu.getMessageid()));
			}
			TUser send = us.getUserDetail(me.getSendcode());
			me.setTo(list);
			me.setFrom(Long.valueOf(send.getMessageid()));
			me.setName(ts.getGradeClassByClasscode(me.getReceivecode()));
			me.setSendType(1);
			break;
		case 5:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("userid"));
			me.setIdatetime(new Date().getTime()/1000);
			TUser re = us.getUserDetail(me.getReceivecode());
			TUser se = us.getUserDetail(me.getSendcode());
			list.add(Long.valueOf(re.getMessageid()));
			me.setTo(list);
			me.setFrom(Long.valueOf(se.getMessageid()));
			me.setName(se.getAlias());
			me.setSendType(1);
			break;
		case 6:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("teacherid"));
			if(ids.length>0){
				appidList = new ArrayList<Integer>();
				for (String id : ids) {
					appidList.add(Integer.parseInt(id));
				}
			}
			me.setIdatetime(new Date().getTime()/1000);
			map = us.getTeachersMess(me.getReceivecode());
			list = map.get("messageids");
			me.setCellphones(map.get("cellphones"));
			me.setTo(list);
			me.setFrom(0L);
			me.setName("学校通知");
			me.setSendType(1);
			break;
		case 7:
			appidList = new ArrayList<Integer>();
			appidList.add(appids.get("userid"));
			me.setIdatetime(new Date().getTime()/1000);
			map = us.getStudentsMess(me.getReceivecode());
			list = map.get("messageids");
			me.setCellphones(map.get("cellphones"));
			me.setTo(list);
			me.setFrom(0L);
			me.setName("学校通知");
			me.setSendType(1);
		default:
			break;
		}
		return me;
		
	}
	public static String sendToClient(String content,String param,List<Long> to,int type,int channel,Long from){
		String result = "";
		try {
			for (Integer appid:appidList) {
				Date now = new Date();
				MessageData md = new MessageData();
				md.setFrom(from);
				md.setTime(now.getTime()/1000);
				md.setAppid(appid);
				md.setType(type);
				md.setText(content);
				Payload pl = new Payload();
				Aps aps = new Aps();
				aps.setBadge(1);
				aps.setCategory("category");
				aps.setContentav_ailable(false);
				aps.setSound("");
				pl.setAps(aps);
				pl.setGeneral("");
				pl.setExtras(param);
				md.setPayload(pl);
				//选平台的 PF_ALL = 1,PF_DESKTOP = 2,PF_ANDROID = 4,PF_IOS = 8,PF_WEBPAGE = 16
				md.setPlatform(1);
				md.setTo(to);
				String json = GsonUtils.getInstance().toJson(md);
				String st = HttpUtil.getInstance().doPostJson(ConfigUtil.getProperty("message","url")+"push", json);
				MResult pr = (MResult) GsonUtils.getInstance().fromJson(st, MResult.class);
				Long a = pr.getData();
				result = result+appid +"," +a+"#";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.substring(0, result.length()-1);
	}
	public static String getEncoding(String str) {      
		String encode = "UTF-8";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s2 = encode;      
	              return s2;      
	           }      
	       } catch (Exception exception2) {      
	       }     
	        encode = "GB2312";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s = encode;      
	              return s;      
	           }      
	       } catch (Exception exception) {      
	       }      
	       encode = "ISO-8859-1";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s1 = encode;      
	              return s1;      
	           }      
	       } catch (Exception exception1) {      
	       }      
	       encode = "GBK";      
	      try {      
	          if (str.equals(new String(str.getBytes(encode), encode))) {      
	               String s3 = encode;      
	              return s3;      
	           }      
	       } catch (Exception exception3) {      
	       }      
	      return "";      
	   }      
}
